<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>绑定账号</title><meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="author" content="duanliang, duanliang920.com" />
    <style>
        body{font-size:62.5%;font-family:"Microsoft YaHei",Arial; overflow-x:hidden; overflow-y:auto;}
        .viewport{ max-width:640px; min-width:300px; margin:0 auto;}
    </style>

    <script src="../js/jweixin-1.2.0.js"></script>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp"></script>

    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
    <link rel="stylesheet" href="../css/weui.min.css">
    <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>
    <!-- 如果使用了某些拓展插件还需要额外的JS -->
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/swiper.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/city-picker.min.js"></script>
    <link rel="stylesheet" href="../css/style.default.css" type="text/css" />
    <link rel="stylesheet" media="screen" href="../../css/style.ie9.css"/>
    <link rel="stylesheet" media="screen" href="../../css/style.ie8.css"/>
    <script src="../js/css3-mediaqueries.js"></script>
    <link rel="stylesheet" href="../css/weui.css"/>

    <script src="../js/zepto.min.js"></script>
    <script src=../js/config.js ></script>
    <script>
        var url1="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ configAppId+"&redirect_uri=";
        var url2="&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        var openMap=url+"/page/openMap";
        $(document).ready(function () {
            var openId;
            $.getJSON("/map/getConfig/", function (json) {
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: configAppId, // 必填，公众号的唯一标识
                    timestamp: json.timeStamp, // 必填，生成签名的时间戳
                    nonceStr: json.nonceStr, // 必填，生成签名的随机串
                    signature: json.signature,// 必填，签名
                    jsApiList: [
                        'openLocation',
                        'getLocation',
                        'closeWindow'
                    ] // 必填，需要使用的JS接口列表，所有JS接口列表见官方文档附录2
                });
                wx.ready(function () {
                    console.log("this is ready");
                });
                wx.error(function (res) {
                    console.log("this is error");
                });
            });
            $.getJSON("/map/getOpenId/", function (json) {
                openId = json.openId;
                var user={openId:openId};
                $.ajax({
                    type: "get",
                    url: urlToUser+"/user/getUserByOpenId1/"+openId,
                    data: user,
                    dataType: "json",
                    success: function (data) {
                        if (data == "" || data == null) {
                            return;
                        }
                        else {
                            alert("该微信号已经绑定过自在天原账号！");
                            window.location.replace(url1+openMap+url2);
                        }
                    }
                })
            });
            $("#b_submit").click(function () {
                var username = $("#username").val();
                var password = $("#password").val();
                var user = {
                    name: username,
                    password: password,
                    openId:openId
                };
                $.ajax({
                    type: "post",
                    url: urlToUser+"/user/checkUser",
                    data: user,
                    success: function (data) {
                        if ("false1" == data.result) {
                            alert("用户名不存在！");
                            return;
                        }
                        if ("false2" == data.result) {
                            alert("用户名与密码不匹配！");
                            return;
                        }
                        if ("success" == data.result) {
                            updateOpenIdToUser(user);
                            addAbility(user);
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                });
            });

            function updateOpenIdToUser(data) {
                $.ajax({
                    type: "post",
                    url: urlToUser+"/user/updateOpenIdToUser",
                    data:data,
                    dataType:"json",
                    async:false,
                    success:function(data) {
                        if("success"==data.result){
                            alert("绑定成功！");
                            window.location.replace(url1+openMap+url2);
                        }
                        if("false"==data.result){
                            alert("绑定失败！");
                        }
                    },
                    error:function(err) {
                        console.log(err);
                    }
                });

            }
            function addAbility(data) {
                $.ajax({
                    type:"get",
                    url:"/ability/addAbilityByUserId",
                    data:data,
                    dataType:"json",
                    async:false,
                    success:function(result) {
                        console.log(result);
                    },
                    error:function(err) {
                        console.log(err);
                    }
                });
            }
            $("#b_cancel").click(function () {
                wx.closeWindow();
            })
        });
    </script>
</head>
<body class="loginpage">
<div class="loginbox" >
    <div class="loginboxinner" >
        <div class="logo">
            <h1 class="logo" >自在天原</h1>
            <span class="slogan"  >天行健管理系统</span>
        </div><!--logo-->
        <br clear="all" /><br />
        <div class="username">
            <div class="usernameinner"  >
                <input  type="text" name="username" id="username" placeholder="用户名"/>
            </div>
        </div>
        <div class="password">
            <div class="passwordinner">
                <input type="password"  name="password" id="password" placeholder="密码"/>
            </div>
        </div>
        <button  style="color:#FFFFFF;" id="b_submit">确定</button>
        <button  style="color:#FFFFFF;" id="b_cancel" >取消</button>
    </div><!--loginboxinner-->
</div><!--loginbox-->
</body>
<style>
    /*#b_submit{*/
        /*position: relative;*/
        /*left:30px;*/
        /*width: 80px;*/
    /*}*/
    #b_cancel{
        position: relative;
        top:20px;
        background: #a8a8a8;
    }


</style>
</html>